﻿using System;
using System.Collections.Generic;
using System.Linq;
using Contracts.Entities;

namespace Model.Movie
{
    public class SynchronizeMovieItems
    {
        public void Process(IEnumerable<MovieItem> directoryItems, IEnumerable<MovieItem> databaseItems)
        { 
            var syncResults = new List<MovieItem>();

            foreach (var dbItem in databaseItems)
            {
                var directoryItem = directoryItems.SingleOrDefault(m => m.FolderPath == dbItem.FolderPath);

                if (directoryItem == null)
                    dbItem.SyncState = SyncState.Deleted;

                syncResults.Add(dbItem);            
            }

            foreach (var directoryItem in directoryItems)
            {
                if (databaseItems.SingleOrDefault(m => m.FolderPath == directoryItem.FolderPath) == null)
                {
                    directoryItem.SyncState = SyncState.Inserted;
                    syncResults.Add(directoryItem);
                }
            }

            OnCompleted(syncResults);
        }

        public event Action<IEnumerable<MovieItem>> OnCompleted;
    }
}
